﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using School.Entities;
using System.Collections;
using System.IO;

namespace School.Admission
{
    public class Emergency
    {
        private static SchoolEntities db = new SchoolEntities();

        public static Boolean Add(SD_Emergency emergency)
        {


            db.AddToSD_Emergency(emergency);
            db.SaveChanges();
            return true;

        }

        public static SD_Emergency Single(long id)
        {
            return db.SD_Emergency.Single(x => x.Emergency_Id == id);
        }


        public static SD_Emergency SingleByStudentId(long sid)
        {
            try
            {
                return db.SD_Emergency.Single(x => x.Student_Id == sid);
            }
            catch
            {
                return null;
            }
        }



        public static IEnumerable<SD_Emergency> All(Hashtable items)
        {
            SchoolEntities db = new SchoolEntities();

            var query = from emergency in db.SD_Emergency select emergency;
            foreach (DictionaryEntry item in items)
            {
                if (item.Key == "Name1")
                {
                    query = query.Where(c => c.Name1 == item.Value);
                }

                if (item.Key == "Name2")
                {
                    query = query.Where(c => c.Name2 == item.Value);
                }

                if (item.Key == "Name3")
                {
                    query = query.Where(c => c.Name3 == item.Value);
                }

                if (item.Key == "Name4")
                {
                    query = query.Where(c => c.Name4 == item.Value);
                }

                if (item.Key == "Name5")
                {
                    query = query.Where(c => c.Name5 == item.Value);
                }
            }
            return query.ToList();
        }

        public static void Update(SD_Emergency emergency)
        {
            SD_Emergency c = db.SD_Emergency.Single(x => x.Emergency_Id == emergency.Emergency_Id);

            c.Emergency_Id = emergency.Emergency_Id;
            c.Student_Id = emergency.Student_Id;
            c.Name1 = emergency.Name1;
            c.Name2 = emergency.Name2;
            c.Name3 = emergency.Name3;
            c.Name4 = emergency.Name4;
            c.Name5 = emergency.Name5;
            c.Phone1 = emergency.Phone1;
            c.Phone2 = emergency.Phone2;
            c.Phone3 = emergency.Phone3;
            c.Phone4 = emergency.Phone4;
            c.Phone5 = emergency.Phone5;
            c.MobilePhone1 = emergency.MobilePhone1;
            c.MobilePhone2 = emergency.MobilePhone2;
            c.MobilePhone3 = emergency.MobilePhone3;
            c.MobilePhone4 = emergency.MobilePhone4;
            c.MobilePhone5 = emergency.MobilePhone5;



            db.SaveChanges();

        }

        public static Boolean Delete(long id)
        {
            try
            {
                db.SD_Emergency.DeleteObject(db.SD_Emergency.Single(x => x.Emergency_Id == id));
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

    }
}
